package cn.stylefeng.guns.modular.order.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.goods.service.LiuGoodsService;
import cn.stylefeng.guns.modular.order.constant.OrderConstant;
import cn.stylefeng.guns.modular.order.entity.TbOrderApi;
import cn.stylefeng.guns.modular.order.model.params.TbOrderParam;
import cn.stylefeng.guns.modular.order.model.result.TbOrderResult;
import cn.stylefeng.guns.modular.order.service.TbOrderService;
import cn.stylefeng.guns.modular.tao.api.AESCoder;
import cn.stylefeng.guns.modular.tao.api.ApiGetToken;
import cn.stylefeng.guns.modular.tao.api.Config;
import cn.stylefeng.guns.modular.tao.api.HttpRequest;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * ������
 *
 * @author zhou
 * @Date 2019-08-02 17:37:10
 */
@Controller
@RequestMapping("/tbOrder")
public class TbOrderController extends BaseController {
    private final static Logger LOGGER = LoggerFactory.getLogger(TbOrderController.class);
    private static String PREFIX = "/order/tbOrder";

    @Autowired
    private TbOrderService tbOrderService;


    @Autowired
    private LiuGoodsService liuGoodsService;



    /**
     * ��ת����ҳ��
     *
     * @author zhou
     * @Date 2019-08-02
     */
    @RequestMapping("")
    public String index() {
        LOGGER.info("��ת����ҳ��");
        return PREFIX + "/tbOrder.html";
    }

    /**
     * ����ҳ��
     *
     * @author zhou
     * @Date 2019-08-02
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/tbOrder_add.html";
    }

    /**
     * �༭ҳ��
     *
     * @author zhou
     * @Date 2019-08-02
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/tbOrder_edit.html";
    }

    /**
     * �����ӿ�
     *
     * @author zhou
     * @Date 2019-08-02
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(TbOrderParam tbOrderParam) {
        this.tbOrderService.add(tbOrderParam);
        return ResponseData.success();
    }



    @RequestMapping("/test")
    @ResponseBody
        public String weiLogin(String code){
            System.out.println(code);
            String requestUrl = "https://api.weixin.qq.com/sns/jscode2session";
            HttpRequest hr=new HttpRequest();
            JSONObject jsonObject = JSON.parseObject(hr.sendPost(requestUrl,"appid=wx2c10cd59f4e41a3a&secret=d8e7153744270e36b1d922afcf7f320c&js_code="+code+"&grant_type=authorization_code"));

            String token = (String)jsonObject.get("openid");
            return  token;

           // System.out.println(jsonObject);

           // String url="https://api.weixin.qq.com/cgi-bin/token";
       // jsonObject= JSON.parseObject(hr.sendPost(url,"appid=wx2c10cd59f4e41a3a&secret=d8e7153744270e36b1d922afcf7f320c&grant_type=client_credential"));
       // System.out.println("�鿴��" + !jsonObject.containsKey("access_token"));
      //  String token = (String)jsonObject.get("access_token");
        //  return  token;
        }

    /**
     * �༭�ӿ�
     *
     * @author zhou
     * @Date 2019-08-02
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(TbOrderParam tbOrderParam) {
        this.tbOrderService.update(tbOrderParam);
        return ResponseData.success();
    }

    /**
     * ɾ���ӿ�
     *
     * @author zhou
     * @Date 2019-08-02
     */
    @RequestMapping("delete")
    @ResponseBody
    public ResponseData delete(TbOrderParam tbOrderParam) {
        this.tbOrderService.delete(tbOrderParam);
        return ResponseData.success();
    }

    /**
     * �鿴����ӿ�
     *
     * @author zhou
     * @Date 2019-08-02
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(TbOrderParam tbOrderParam) {
        String[] name = tbOrderParam.getOrderId().split(",");
        tbOrderParam.setOrderId(name[0]);
        tbOrderParam.setGoodsId(name[1]);
        TbOrderResult detail = this.tbOrderService.detail(tbOrderParam);
        return ResponseData.success(detail);
    }

    /**
     * ���붩��

     * @author zhou
     * @Date 2019-08-02
     */
    @RequestMapping("/uploadDetail")

    public String  uploadDetail(@RequestParam("myArray") List<String> myArray,@RequestParam("status")String status) throws Exception {


       List<TbOrderApi> ta=this.tbOrderService.uploadDetail(myArray);
        for(TbOrderApi t:ta){
            System.out.println(t.getUserId());
            t.setEshopbuyer( this.tbOrderService.selectWxUser(t.getUserId()));
        }
     System.out.println(JSON.toJSONString(ta));
        HttpRequest hr=new HttpRequest();
        String rets = "";
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//�������ڸ�ʽ
        //��ȡtoken
        ApiGetToken apiGetToken = new ApiGetToken();
        String token = apiGetToken.DoGetToken();
        AESCoder coder = new AESCoder();
        Map<String,String> param = new HashMap<>();
        param.put("shopkey",Config.shop_key);
        param.put("orders",JSON.toJSONString(ta));
        param.put("method", "zyx.selfbuiltmall.uploadsaleorders");
        param.put("appkey", Config.appkey);
        param.put("timestamp",df.format(new Date()));
        param.put("token", token);
        param.put("sign", coder.SignRequest(param, Config.sign_key));
        //post����
        String postString = "";
        for (String in : param.keySet()) {
            postString += in + "=" + URLEncoder.encode(param.get(in).toString(), "utf-8") + "&";
        }


        postString = postString.substring(0, postString.length() - 1);
        System.out.println(postString+"skdjskladhksjahdkjsadh");
        rets = HttpRequest.sendPost(Config.api_link, postString);
        System.out.println(rets+"kjsdahkjsahfdkjsahkjdfhsabkjdhsakjhdkjsahdkjsabhdjksabdkjsbajdkbsakjdbsajkd");
        //String result= hr.sendPost("http://ca.mygjp.com:8002/api/",body.toString());
       // System.out.println(result);

        this.tbOrderService.updateStatus(myArray,status);
        return PREFIX + "/tbOrder.html";
    }

    /**
     * ��ѯ�б�
     *
     * @author zhou
     * @Date 2019-08-02
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(String condition) {
        return this.tbOrderService.findPageBySpec(condition);
    }

    /**
     * ����
     * @param tbOrderParam
     * @return
     */
    @RequestMapping("/deliver")
    @ResponseBody
    public ResponseData deliver(TbOrderParam tbOrderParam) {
        tbOrderParam.setStatus(OrderConstant.ORDER_STATUS_SHIPPED);
        this.tbOrderService.deliver(tbOrderParam);
        return ResponseData.success();
    }

    /**
     * �������
     */
    @RequestMapping("/addOrder")
    @ResponseBody
    public void addOrder(TbOrderParam tbOrderParam){

        String a="1,2,3,4";
        String[] result1 = a.split(",");
        int b=0;
      for (HashMap<String,String> map : liuGoodsService.selectGossId(tbOrderParam)) {
            tbOrderParam.setOrderId(result1[b]+"100");
            tbOrderParam.setGoodsId(map.get("goodsNumber"));
            tbOrderParam.setGoodsName(map.get("goodsName"));
            this.tbOrderService.addOrder(tbOrderParam);
             map.put("inventoryQuantity","1");
             map.put("goodsNumber",result1[b]);
            liuGoodsService.updateInventoryQuantity(map);
        b++;
        }
    }


    /**
     * ��ѯ�б�
     *
     * @author zhou
     * @Date 2019-08-02
     */
    @ResponseBody
    @RequestMapping("/updateS")
    public void updateS(TbOrderParam tbOrderParam) {

        this.tbOrderService.updateS(tbOrderParam);
    }

    /**
     * 退货
     */
    @RequestMapping("/updateOrder")
    @ResponseBody
    public void updateOrder(TbOrderParam tbOrderParam){
        tbOrderService.updateOrders(tbOrderParam);
    }


}


